package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.dto.*;
import com.eastfair.pay.entity.Contract;
import com.eastfair.pay.service.ContractService;
import com.eastfair.pay.vo.ContractFinanceVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 合同
 * </p>
 * 目前只有场馆在使用
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/contract")
@Api(value = "Contract", tags = "合同")
@PreAuth(replace = "pay:contract:")
public class ContractController extends SuperController<ContractService, Long, Contract, ContractPageQuery, ContractSaveDTO, ContractUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Resource
    private ContractService contractService;
    @Override
    public void handlerResult(IPage<Contract> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Contract> contractList = list.stream().map((map) -> {
            Contract contract = Contract.builder().build();
            //TODO 请在这里完成转换
            return contract;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(contractList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ContractPageQuery> params) {
        ContractPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 保存合同
     * @param contractDTO
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody ContractDTO contractDTO){
        try {
            contractService.saveOrUpdate(contractDTO);
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 合同付款计划是否认款
     * @return
     */
    @GetMapping("/isCollected/{contractId}")
    public R isCollected(@PathVariable("contractId") Long contractId){
        try {
            Boolean isCollected = contractService.isCollected(contractId);
            return R.success(isCollected);
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 合同付删除
     * @return
     */
    @GetMapping("/delete/{contractId}")
    public R delete(@PathVariable("contractId") Long contractId){
        try {
            contractService.deleteById(contractId);
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 合同财务信息
     * @return
     */
    @GetMapping("/queryFinance/{contractId}")
    public R<ContractFinanceVO> queryFinance(@PathVariable("contractId") Long contractId){
        try {
            return R.success(contractService.queryFinance(contractId));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

}
